package com.ls.lserp.modules.material.service;


import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import com.drew.lang.StringUtil;
import com.ls.lserp.common.persistence.Page;
import com.ls.lserp.common.service.CrudService;
import com.ls.lserp.common.utils.CollectionsUtils;
import com.ls.lserp.common.utils.ResourcesUtil;
import com.ls.lserp.common.utils.SpringContextHolder;
import com.ls.lserp.modules.material.dao.InventoryRequestDao;
import com.ls.lserp.modules.material.entity.InventoryRequest;
import com.ls.lserp.modules.setting.dao.OgnizationDao;
import com.ls.lserp.modules.setting.entity.Ognization;
import com.ls.lserp.modules.trade.entity.Enchase;

/**
 * @ClassName: InventoryRequestService
 * @Description: 物料入库申请
 * 
 * @author 
 * @date 2017年04月14日
 *
 */

@Service
@Transactional(readOnly = false)
public class InventoryRequestService extends CrudService<InventoryRequestDao,InventoryRequest>{

    @Autowired
    private InventoryRequestDao inventoryRequestDao;
    
    @Autowired
    private OgnizationDao ognizationDao;
    
	/**
	 * @Title: findList
	 * @Description: 一览查询
	 * @param page
	 * @param orderMateriel
	 * @return Page<OrderMateriel>
	 */
	public Page<InventoryRequest> findList(Page<InventoryRequest> page, InventoryRequest inventoryRequest) {
		
		List<InventoryRequest> irDetailList = null;
		BigDecimal tempRequestQuantity = null;
		BigDecimal tempRealQuantity = null;
		BigDecimal tempResidueQuantity = null;
		
		// tab页分，检索条件设定(0:全部,1:待入库,2:已入库)
		inventoryRequest.setInventoryStatus(ResourcesUtil.getConfig("inventory_status_wait"));
		
		// 入出库区分（入库)
		inventoryRequest.setInventoryInOutKbn(ResourcesUtil.getConfig("inventory_in_out_kbn_in"));
		
		// 入库类别（客供入库）
		inventoryRequest.setInventoryCategory(ResourcesUtil.getConfig("inventory_category_customer"));
		
		// 设置分页参数
		inventoryRequest.setPage(page);
		List<InventoryRequest> resultList = inventoryRequestDao.findList(inventoryRequest);
		
		if(!CollectionsUtils.isEmpty(resultList)){
			// 库存申请信息
			for(InventoryRequest ir : resultList){
				tempRequestQuantity = new BigDecimal("0");
				tempRealQuantity = new BigDecimal("0");
				tempResidueQuantity = new BigDecimal("0");
				irDetailList = inventoryRequestDao.findInventoryRequestDetailList(ir);
				if(!CollectionsUtils.isEmpty(irDetailList)){
					// 库存申请明细
					for(InventoryRequest ird : irDetailList){
						tempRequestQuantity = tempRequestQuantity.add(checkBigDecimalNull(ird.getRequestQuantity()));
						tempRealQuantity = tempRealQuantity.add(checkBigDecimalNull(ird.getRealQuantity()));
					}
					tempResidueQuantity = tempRequestQuantity.subtract(tempRealQuantity);
					if (tempResidueQuantity.compareTo(BigDecimal.valueOf(0)) == -1) {
						tempResidueQuantity = new BigDecimal("0");
					}
					ir.setRequestQuantity(tempRequestQuantity);
					ir.setRealQuantity(tempRealQuantity);
					ir.setResidueQuantity(tempResidueQuantity);
				}
			}
		}
		
        // 执行分页查询
        page.setList(resultList);
        
        return page;
	}
	
	/**
	 * check BigDecimal
	 * @param cost
	 * @return 取不到返回0
	 */
	public BigDecimal checkBigDecimalNull(BigDecimal cost){
		if(cost==null){
			return new BigDecimal("0");
		}
		return cost;
	}
	
    /**
     * @Title: selVenderList
     * @Description: 查询
     * @param @param ognization
     * @param @return
     * @return Page<ognization> 
     * @throws
     */
   public List<Ognization> selVenderList(Ognization ognization) {
       
       return ognizationDao.findList(ognization);
   }
   
   /**
    * @Title: findBrand
    * @Description: 查询
    * @param @param venderId
    * @param @return
    * @return List<InventoryRequest>
    * @throws
    */
  public List<InventoryRequest> findBrand(String venderId) {
	  InventoryRequest paramIr = new InventoryRequest();
	  paramIr.setVenderId(Long.parseLong(venderId));
      return inventoryRequestDao.findBrandList(paramIr);
  }
  
  /**
   * @Title: saveInventoryRequest
   * @Description: 保存入库申请
   * @param @param inventoryRequest
   * @return void 
   * @throws
   */
  @Transactional(readOnly = false)
  public void saveInventoryRequest(InventoryRequest inventoryRequest) {
  	
  	// 判断新建编辑
  	//若为null 执行插入语句（新建）
  	if (inventoryRequest.getId()==null){			
  		inventoryRequest.preInsert();
  		inventoryRequest.setVersion(1L);
  		inventoryRequest.setDeleteFlag(InventoryRequest.DEL_FLAG_NORMAL);
  		inventoryRequest.setEnableFlag(InventoryRequest.ENABlE_FLAG_NORMAL);
  		inventoryRequest.setInventoryStatus(ResourcesUtil.getConfig("inventory_status_wait"));
  		inventoryRequest.setInventoryInOutKbn(ResourcesUtil.getConfig("inventory_in_out_kbn_in"));
  		inventoryRequestDao.insertInventoryRequest(inventoryRequest);
  	}else{
		// 更新数据
  		inventoryRequest.preUpdate();
  		inventoryRequestDao.updateInventoryRequest(inventoryRequest);
		// 删除入库单明细
  		inventoryRequestDao.deleteRequestDetails(inventoryRequest);
  	}
  	
  	
  	// 判断list是否为空
  	if(!CollectionsUtils.isEmpty(inventoryRequest.getRequestDetailsList())){
  	   // 遍历赋值
         for(InventoryRequest itr : inventoryRequest.getRequestDetailsList()){
        	 itr.setId(inventoryRequest.getId());
        	 itr.preInsert();
        	 itr.setVersion(1L);
        	 itr.setDeleteFlag(InventoryRequest.DEL_FLAG_NORMAL);
        	 itr.setEnableFlag(InventoryRequest.ENABlE_FLAG_NORMAL);
        	 itr.setStockinStatus(ResourcesUtil.getConfig("inventory_status_wait"));
        	 inventoryRequestDao.insertInventoryRequestDetails(itr);
  		}
  	}
  }
  
  /**
   * @Title: findRequestDetailsList
   * @Description: 入库申请明细
   * @param @param inventoryRequest
   * @param @return
   * @return List<InventoryRequest>
   * @throws
   */
 
 public List<InventoryRequest> findRequestDetailsList(InventoryRequest inventoryRequest) {
     return inventoryRequestDao.findRequestDetailsList(inventoryRequest);
 }
}